//
//  AAALHXDateHandle.m
//  UI_豆瓣影院
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AAALHXDateHandle.h"

@implementation AAALHXDateHandle

+ (AAALHXDateHandle *)sharedDateBaseHandle {
    
    // 静态修饰指针
    static AAALHXDateHandle *handle = nil;
    // 线程只执行一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        handle = [[AAALHXDateHandle alloc] init];
    });
    
    return handle;
}

//  打开数据库
- (void)openDB {

    //  沙盒路径
    NSString *doument = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@",doument);
    //  拼接数据库拼接
    NSString *dbPath = [doument stringByAppendingPathComponent:@"lhxStudent.sqlite"];
    NSLog(@"%@", dbPath);
    // 打开数据库
    int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    } else {
        NSLog(@"打开失败");
    }
    
}

- (void)createTable {
    // sql 语句
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists Activity(number integer primary key autoincrement, title text)"];
    
    // 让数据库执行的语句，数据库必须是打开的
    [self execcuteSQLString:sqlStr showInfo:@"建表"];
  
    
    NSString *sqlStr1 = [NSString stringWithFormat:@"create table if not exists CinemaModel(number integer primary key autoincrement, title text)"];
    [self execcuteSQLString:sqlStr1 showInfo:@"建表"];
   
    NSString *sqlStr2 = [NSString stringWithFormat:@"create table if not exists myTable(number integer primary key autoincrement, name text, pwd text, type integer)"];
    [self execcuteSQLString:sqlStr2 showInfo:@"建表"];
}

- (void)insertModel:(ActiveModel *)model {
   // sql 语句
   
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Activity(title) values('%@')", model.title];
    
    [self execcuteSQLString:sqlStr showInfo:@"插入"];
}

- (void)insertModel1:(CinemaModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into CinemaModel(title) values('%@')", model.title];
   [self execcuteSQLString:sqlStr showInfo:@"插入"];
    
}

- (void)insertModel2:(myModel *)model {
    
    NSString *sqlStr = [NSString stringWithFormat:@"insert into myTable(name, pwd, type) values('%@','%@', '%ld')", model.name, model.pwd, model.type];
    
    [self execcuteSQLString:sqlStr showInfo:@"插入"];}


- (NSMutableArray *)selectTable {
    // sql 语句
    NSString *sqlStr = [NSString stringWithFormat:@"select * from Activity"];
    // 创建一个跟随指针
    sqlite3_stmt * stmt = nil;
    // 进入查询
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    // 创建一个数组
    NSMutableArray *arrayModel = [NSMutableArray array];
    if (result == SQLITE_OK) {
    
    
    // 遍历
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        const unsigned char *title = sqlite3_column_text(stmt, 1);
        ActiveModel *model = [ActiveModel actionModel];
        model.title = [NSString stringWithUTF8String:(const char *)title];
        [arrayModel addObject:model.title];
        
    }
    } else {
        NSLog(@"查询失败");
    }
        

    return arrayModel;
    
    
}

- (NSMutableArray *)selectTable1 {
    
    // sql 语句
    NSString *sqlStr = [NSString stringWithFormat:@"select * from CinemaModel"];
    // 创建一个跟随指针
    sqlite3_stmt * stmt = nil;
    // 进入查询
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    // 创建一个数组
    NSMutableArray *arrayModel = [NSMutableArray array];
    if (result == SQLITE_OK) {
        
        
        // 遍历
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            CinemaModel *model = [CinemaModel cinemaModel];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            [arrayModel addObject:model.title];
        }
    } else {
        NSLog(@"查询失败");
    }
    
    
    return arrayModel;
    
    

}
- (NSMutableArray *)selectTable2{
    
    // sql 语句
    NSString *sqlStr = [NSString stringWithFormat:@"select * from myTable"];
    // 创建一个跟随指针
    sqlite3_stmt * stmt = nil;
    // 进入查询
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    // 创建一个数组
    NSMutableArray *arrayModel = [NSMutableArray array];
    if (result == SQLITE_OK) {
                // 遍历
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *pwd = sqlite3_column_text(stmt, 2);
            int type = sqlite3_column_int(stmt, 3);
            // 创建model 给model 赋值
            myModel *model = [myModel model];
            model.name = [NSString stringWithUTF8String:(const char *)name];
            model.pwd = [NSString stringWithUTF8String:(const char *)pwd];
            model.type = type;
            [arrayModel addObject:model];
        }
        return arrayModel;
    }
//    else {
//        NSLog(@"查询失败");
//    }
//    
    
    return 0;
 
}
- (void)updataTable:(myModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"update myTable set type = %ld" ,model.type];
    [self execcuteSQLString:sqlStr showInfo:@"更新"];
    
}



- (void)deleteModel:(NSString *)string {
    
    // sql 语句
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Activity where title = '%@'", string];
    // 执行
    [self execcuteSQLString:sqlStr showInfo:@"删除"];
}
- (void)deleteModel1:(NSString *)string {
    NSString *sqlStr =[NSString stringWithFormat:@"delete from CinemaModel where title = '%@'", string];
    [self execcuteSQLString:sqlStr showInfo:@"删除"];
    
}

- (void)deleteModel2:(NSString *)string {
    NSString *sqlStr = [NSString stringWithFormat:@"delete from myTable where name = 'liu'"];
    [self execcuteSQLString:sqlStr showInfo:@"删除"];
    
}

- (void)deleteALLModel {
    NSString *sqlStr =[NSString stringWithFormat:@"delete from Activity"];
    [self execcuteSQLString:sqlStr showInfo:@"清空"];
    
    NSString *sqlStr1 =[NSString stringWithFormat:@"delete from CinemaModel"];
    [self execcuteSQLString:sqlStr1 showInfo:@"清空"];
    
    
    NSString *sqlStr2 = [NSString stringWithFormat:@"delete from myTable"];
    [self execcuteSQLString:sqlStr2 showInfo:@"清空"];
    
}

- (void)execcuteSQLString:(NSString *)sqlStr showInfo:(NSString *)info {
    
    BOOL result =  sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    NSString *str = @"";
    if (result == SQLITE_OK) {
        str = [NSString stringWithFormat:@"%@ success", info];
    } else {
        str = [NSString stringWithFormat:@"%@ failed", info];
    }
   
    
    
    
    
}



@end
